import React, { PureComponent } from 'react';
import { Input, notification } from 'antd';
import { connect } from 'dva';
import Clipboard from 'clipboard';
import "./form.less"
const { TextArea } = Input;
class OtherLines extends PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            type:null,
        }
        this.getData = this.getData.bind(this)
        this.copyText=this.copyText.bind(this)
    }
    async getData() {
        let { extra, type } = this.props;
        let res = await this.props.RoadlineOriginData({ roadlineId: extra.roadId })
        this.setState({
            data: res,
            type
        })
    }
    copyText(){
        let res = new Clipboard(document.getElementById("textArea"));
      res.on("success", function() {
        notification.info({message:"已复制到剪切板"});
        res.destroy();
      });
      res.on("error", function() {
        notification.warn({message:"复制到剪切板失败"});
        res.destroy();
      });
    }
    componentDidMount() {
        if (this.props.onLoaded) {
            this.props.onLoaded(this);
        }
        this.getData()
        this.copyText()

    }
    render() {
        let { type } = this.props;
        let {data}=this.state;
        let value;
        if(data&&data.length){
            value=data.find(item=>item.attrName==type).attrValue
        }
        return (
            <TextArea id="textArea"   readOnly
            data-clipboard-action="copy" data-clipboard-target="#textArea"
            style={{height:"calc(100% - 40px)",resize:"none"}} value={value} rows={12} />
        )
    }
}
export default connect(
    ({ }) => ({}),
    dispatch => ({
        RoadlineOriginData: (params) => dispatch({
            type: 'refRoads/RoadlineOriginData',
            payload: params
        })
    })
)(OtherLines);